function WeatherDiscMan(){
	var currentWeather = "ice";
	var currentWeatherChinese = "覆冰";
	
	var currentPanel = 0;
	
	var endYear = new Date().getFullYear();
	var currentMonth = 1;
	var currentYear = '';
	var faultSum = 0;
	
	var rightDivId = 'affectObjDiv';
	var bottomTitleId = "bottom_title";
	var bottomDivId = 'bottomDiv';
	var timelineId = 'bottomTimeline';
	var leftTopDivId = 'leftTopDiv';
	var leftTopTitleId = 'leftTopDivTitle';
	
	var tableId;
	var currentYearId = 'weatherCurrentYear';
	var bhDivId = 'weatherBhAct';
	
	var allMonths = [1,2,3,4,5,6,7,8,9,10,11,12];
	var months = [];
	var iceMonths = [1,2,11,12];
	var bufferMonthData = false;//是否缓存月份数据
	var monthData = {};
	var selectVoltages = [['all','全部'],['499','500kV及以上'],['220','220kV'],['110','110kV']];
	var selectedIndex = 1;
	
	var radiusStep = 20;
	var radiusStart = 30;
	var tolerance = 150;
	var pointSteps = [10,50,100,1000,5000,8000,10000];
	
	var mapCluster = undefined;
	var isShow = true;
	
	this.setWeather = function(wea){
		currentWeather = wea;
		if(currentWeather == "ice"){
			currentWeatherChinese = "覆冰";
		}else if(currentWeather = "thunder"){
			currentWeatherChinese = "雷击";
		}
		monthData = {};
		currentYear = new Date().getFullYear();
		return this;
	};
	this.show = function(){
		clearAll(null,'weatherDiscMan');
		isShow = true;
		loadData();
	};
	this.changeYear = function(year){
		if(currentYear != year){
			currentYear = year;
			currentMonth = 1;
			loadData();
		}
	};
	this.nextYear = function(){
		currentYear ++;
		loadData();
	};
	this.lastYear = function(){
		currentYear --;
		loadData();
	};
	this.isShow = function(){
		return isShow;
	};
	this.hide = function(){
		$('#'+rightDivId).css('display','none');
		$('#'+bottomTitleId).css('display','none');
		$('#'+bottomDivId).css('display','none');
		$('#'+leftTopDivId).css('display','none');
		isShow = false;
	};
	this.changeVoltage = function(index){
		selectedIndex = index;
		showLoading();
		loadData();
	};
	this.zoom = function(){
		if(mapCluster){
			mapCluster.remove();
			mapCluster.show();
		}
	};
	this.onPanelClick = function(index){
		if(currentPanel == index){
			currentPanel = '';
		}else{
			currentPanel = index;
		}
	};
	function loadData(){
		monthData = {};
		clearAll(null,'weatherDiscMan');
		isShow = true;
		if(currentWeather == "ice"){
			$.getJSON("/rest/ice/getIceMonthByYear/"+currentYear+"/"+selectVoltages[selectedIndex][0],function(result){
				months = result;
				showCurrentYear();
			});
		}else if(currentWeather = "thunder"){
			$.getJSON("/rest/thunder/getThunerMonthByYear/"+currentYear+"/"+selectVoltages[selectedIndex][0],function(result){
				months = result;
				showCurrentYear();
			});
		}
	}
	function showLeftTopDiv(data){
		var title = $('#'+leftTopTitleId);
		title.html(currentWeatherChinese+'历史');
		var div = $('#'+leftTopDivId);
		div.css('display','block');
	}
	function showCurrentYear(){
		if( !months || months.length == 0){
			showNoMonth();
			mapCluster = null;
		}else{
			currentMonth = months[0];
			showCurrentMonth();
		}
		drawBottomDiv();
	}
	function showCurrentMonth(){
		var data = monthData[currentMonth];
		if(data){
			drawMapPoint(data.map,currentWeather);
			drawRightDiv(data.equipments,data.ratios,currentWeather);
		}else{
			showLoading();
			loadMonthData();
		}
	}
	function showLoading(){
		PanelTablesUtil.showLoading(rightDivId,currentYear+"年"+currentMonth+"月"+currentWeatherChinese+'列表');
	}
	function loadMonthData(){
		if(bufferMonthData){
			monthData[currentMonth] = {};
		}
		if(currentWeather == "ice"){
			$.getJSON("/rest/ice/getIcePointByYearAndMonth/"+currentYear+"/"+currentMonth+"/"+selectVoltages[selectedIndex][0],function(result){
				if(bufferMonthData){
					monthData[currentMonth].map = result;
				}
				drawMapPoint(result,'ice');
			});
			$.getJSON("/rest/ice/getIceDiscDataByYearAndMonth/"+currentYear+"/"+currentMonth+"/"+selectVoltages[selectedIndex][0],function(result){
				if(bufferMonthData){
					monthData[currentMonth].equipments = result.equipments;
					monthData[currentMonth].ratios = result.ratios;
				}
				drawRightDiv(result.equipments,result.ratios,'ice');
			});
		}else if(currentWeather == "thunder"){
			$.getJSON("/rest/thunder/getThunderPointByYearAndMonth/"+currentYear+"/"+currentMonth+"/"+selectVoltages[selectedIndex][0],function(result){
				if(bufferMonthData){
					monthData[currentMonth].map = result;
				}
				drawMapPoint(result,'thunder');
			});
			$.getJSON("/rest/thunder/getThunderDiscDataByYearAndMonth/"+currentYear+"/"+currentMonth+"/"+selectVoltages[selectedIndex][0],function(result){
				if(bufferMonthData){
					monthData[currentMonth].equipments = result.equipments;
					monthData[currentMonth].ratios = result.ratios;
				}
				drawRightDiv(result.equipments,result.ratios,'thunder');
			});
		}
	}
	function drawMapPoint(data,weather){
		if(weather!=currentWeather){
			//防止连续点击雷击(A)和覆冰(B)时，假设先点击A，A有数据，后点击B，B没有数据。但是B会先调用这个方法，A后调用这个方法。这样会导致B的title显示A的数据。
			return;
		}
		map.removeAllPopup();
		drawHeatPoints(data);
	}
	function drawHeatPoints(data){
		var fs = [];
		for(var i in data.faults){
			fs.push([data.faults[i].lon,data.faults[i].lat]);
		}
		mapCluster = new MapCluster(data.points,fs);
		mapCluster.show();
	}
	function showNoMonth(){
		var panel = new PanelTablesUtil(rightDivId);
		panel.setTitle(currentWeatherChinese+"情况");
		if(currentWeather=="thunder"){
			panel.setTitleTool('<i class="fa fa-bar-chart" title="故障分析"></i>',"weatherDiscMan.openLJDlg()","ldfx");
		}
		var select = '<select onchange="weatherDiscMan.changeVoltage(this.selectedIndex)" style="width:70px;font-style:normal;">';
		for(var i in selectVoltages){
			var s = selectVoltages[i];
			var selected = '';
			if(i == selectedIndex){
				selected = 'selected';
			}
			select = select + '<option '+selected+'>'+s[1]+'</option>';
		}
		select += ' </select>';
		panel.setTitleTool(select,null,"weatherDiscVoltages");
		var info = '本年度一次设备未发生'+currentWeatherChinese+'事件';
		if(currentWeatherChinese == '雷击'){
			info = '本年度一次设备附近未发生雷击事件';
		}
		panel.setInfo(addNoInfo(info));
		panel.show();
	}
	function drawRightDiv(data,ratios,weather){
		if(weather!=currentWeather){
			//防止连续点击雷击(A)和覆冰(B)时，假设先点击A，A有数据，后点击B，B没有数据。但是B会先调用这个方法，A后调用这个方法。这样会导致B的title显示A的数据。
			return;
		}
		var panel = new PanelTablesUtil(rightDivId);
		var dataset = [];
		var title = currentWeatherChinese+'情况';
		panel.setTitle(title);
		if(currentWeather=="thunder"){
			panel.setTitleTool('<i class="fa fa-bar-chart" title="故障分析"></i>',"weatherDiscMan.openLJDlg()","ldfx");
			dataset = getDataset(data);
		}
		if(currentWeather=="ice"){
			dataset = getIceDataset(data);
		}
		var select = '<select onchange="weatherDiscMan.changeVoltage(this.selectedIndex)" style="width:70px;font-style:normal;">';
		for(var i in selectVoltages){
			var s = selectVoltages[i];
			var selected = '';
			if(i == selectedIndex){
				selected = 'selected';
			}
			select = select + '<option '+selected+'>'+s[1]+'</option>';
		}
		select += ' </select>';
		panel.setTitleTool(select,null,"weatherDiscVoltages");
		var eleTitle = currentYear+'年'+currentMonth+"月"+currentWeatherChinese+"情况";
		if(dataset.length>0){
			var warnInfo = "";
			if(currentWeather == 'ice' && iceMonths.indexOf(currentMonth) == -1){
				warnInfo = "<span style='color:#dd4b39;text-align:center;'>非冬季，数据可能有误！</span>";
				panel.setInfo(warnInfo);
			}
			eleTitle += '<span class="badge bg-red pull-right" title="本月受'+currentWeatherChinese+'影响一次设备数">一次设备  '+dataset.length+'</span>';
			tableId = 'weatherTable'+currentWeather;
			var ele = panel.getCollapsePanelEle(tableId+"Parent", eleTitle, currentPanel == 1,'weatherDiscMan.onPanelClick(1);');
			var tb = $("<table id="+tableId+" class='table table-hover'></table>");
			ele.append(tb);
			var myOpt = MyTable.getDefaultOptions();
			myOpt.scrollY = 300;
			myOpt.scrollCollapse = true;
			myOpt.autoWidth = false;
			myOpt.columnDefs = [{ "width": "35%", "targets": 0 },
			                    { "width": "30%", "targets": 1 },
			                    { "width": "35%", "targets": 2 }];
			var col2 = '';
			if(currentWeatherChinese == '雷击'){
				$.ajaxSettings.async = false;
				$.getJSON("/rest/thunder/getThunderRadius",function(radius){
					col2 = "<span title='雷击点在一次设备附近"+radius+"米内'>雷击次数<i style='font-size:small;color:darkgrey;' class='fa fa-question-circle pull-right'></i></span>";
				});
				$.ajaxSettings.async = true;  
			}
			if(currentWeatherChinese == '覆冰'){
				col2 = '最大厚度';
			}
			
			var tab = new MyTable(tb, ["设备名",col2,"<span title='本月"+currentWeatherChinese+"故障/历史"+currentWeatherChinese+"故障'>故障次数 <i style='font-size:small;color:darkgrey;' class='fa fa-question-circle pull-right'></i></span>"], myOpt);
			tab.setDataSet(dataset);
			tab.show();
			tab.onRowClick(function(data){
				if(data){
			    	searchSelectObj(data[0],false,false,data[3]);
				}
			});
			panel.setDangerBox();
			//保护动作情况分析
			var bhDiv = panel.getCollapsePanelEle(bhDivId+"parent", "保护动作情况分析", currentPanel == 2,'weatherDiscMan.onPanelClick(2);');
			drawBhActDiv(bhDiv,ratios);
		}else{
			var ele = panel.getCollapsePanelEle('weatherDiscTableParent', eleTitle, currentPanel == 1,'weatherDiscMan.onPanelClick(1)');
			if(currentWeatherChinese == "雷击"){
				ele.append(addNoInfo("本月一次设备附近未发生雷击事件。"));
			}else{
				ele.append(addNoInfo("本月一次设备未发生"+currentWeatherChinese+"事件。"));
			}
			panel.setSuccessBox();
		}
		panel.show();
	}
	//点击故障分析打开雷电分析页面
	this.openLJDlg=function(){
		MyModal.showModal("/ui/dataview/weatherdatamining/thunderDataMining.xhtml");
	};
	function drawBhActDiv(div,ratios){
		//共发生多少故障，保护动作正确率为多少，当前系统保护状态正常个数：，异常个数：
		var ratioUtil = new ProtectionActRatio();
		ratioUtil.setRatios(ratios);
		ratioUtil.setFaultSum(faultSum);
		ratioUtil.drawDiv(div);
	}
	function getIceDataset(data){
		var ds = [];
		faultSum = 0;
		for(var i in data){
			var d = [data[i].equipment_name,data[i].maxHeight,data[i].currentFaultCount+"/"+data[i].faultCount,data[i].equipment_id];
			faultSum += Number(data[i].currentFaultCount?data[i].currentFaultCount:0);
			ds.push(d);
		}
		return ds;
	}
	function getDataset(data){
		var ds = [];
		data.sort(sortData);
		faultSum = 0;
		for(var i in data){
			var d = [data[i].equipment_name,data[i].weatherCount,data[i].currentFaultCount+"/"+data[i].faultCount,data[i]['equipment_id']];
			faultSum += Number(data[i].currentFaultCount?data[i].currentFaultCount:0);
			ds.push(d);
		}
		function sortData(obj1,obj2){
			if(obj1.weatherCount == obj2.weatherCount){
				if(obj1.currentFaultCount == obj2.currentFaultCount){
					return obj2.faultCount - obj1.faultCount;
				}else{
					return obj2.currentFaultCount - obj1.currentFaultCount;
				}
			}else{
				return obj2.weatherCount - obj1.weatherCount;
			}
		}
		return ds;
	}
	function addNoInfo(text){
		return PanelTablesUtil.getSmileInfo(text);
	};
	function drawBottomDiv(){
		drawBottomTitle();
		$('#'+bottomDivId).css("display",'block');
		if(months && months.length == 0){
			if(currentWeatherChinese == "雷击"){
				$('#'+timelineId).html(addNoInfo("本年度一次设备附近无雷击事件。"));
			}else if(currentWeatherChinese == "覆冰"){
				$('#'+timelineId).html(addNoInfo("本年度一次设备无覆冰事件。"));
			}
		}else{
			timeLineObj = new TimeLine(timelineId);
			timeLineObj.setTimeDatas(getMonths());
			var opt = timeLineObj.getOption().baseOption;
			opt.backgroundColor = 'transparent';
			opt.timeline.controlStyle.show = true;
			opt.timeline.controlStyle.showPlayBtn = false;
			opt.timeline.controlStyle.showNextBtn = true;
			opt.timeline.controlStyle.showPrevBtn = true;
			opt.timeline.currentIndex = currentMonth -1;
			opt.timeline.top = '40%';
			timeLineObj.show();
			timeLineObj.getChart().on('timelinechanged', function (params) {
				currentMonth = allMonths[params.currentIndex];
				showCurrentMonth();
			});
		}
	}
	function getMonths(){
		var ms = [];
		for(var i in allMonths){
			ms[i] = allMonths[i]+"月";
			for(var j in months){
				if(allMonths[i] == months[j]){
					ms[i] = {value:months[j]+"月",symbol:'diamond',symbolSize:16};
					break;
				}
			}
		}
		return ms;
	}
	function drawBottomTitle(){
		var title = $('#'+bottomTitleId);
		title.css('display','block');
		title.html('');
		var ul = $('<div class="btn-group"></div>');
		ul.append('<button class="btn btn-primary" onclick="weatherDiscMan.lastYear()"><i class="fa fa-chevron-left" /></button>');
		ul.append('<button class="btn btn-primary" id="'+currentYearId+'">'+currentYear+'</button>');
		if(currentYear < endYear){
			ul.append('<button class="btn btn-primary" onclick="weatherDiscMan.nextYear()"><i class="fa fa-chevron-right" /></button>');
		}else{
			ul.append('<button class="btn btn-primary disabled"><i class="fa fa-chevron-right"/></button>');
		}
		title.append(ul);
		setTimePicker(currentYearId,'top-right');
	}
	function setTimePicker(divId,pickerPosition){
		var d = new Date();
		d.setFullYear(currentYear, 0, 1);
		$('#'+divId).datetimepicker({
			format : 'yyyy',
			minView : 4,
			pickerPosition:pickerPosition,
			autoclose : true,
			todayHighlight : true,
			startView:4,
			endDate:new Date(),
			initialDate:d
		}).on('changeYear', function(ev){
			weatherDiscMan.changeYear( ev.date.getFullYear());
		});
	}
	function changeMonth(month){
		currentMonth = month;
		showCurrentMonth();
	}
	function getClusterLayerItems(weather,max){
		if(max > pointSteps[pointSteps.length-1]){
			pointSteps.push(max);
		}
		var items = [];
		//60px内会聚成点
		for(var i=0;i<pointSteps.length;i++){
			var radius = radiusStart + radiusStep*(i+1);
			if(radius>tolerance){
				tolerance = radius;
			}
			var offset = -3-i*1.5;
			items.push({
				count:pointSteps[i],
				style:{
					graphic:true,
					externalGraphic:"/resources/img/clusterPoint/"+currentWeather+"Circle.png",
					graphicWidth:radius,
					graphicHeight:radius,
					labelXOffset:offset,
					fontFamily:"隶书",
					fontSize:"15px",
					fontWeight:"bold",
					fontStyle:"italic",
				}
			});
		}
		return items;
	}
};
